﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BLL;
using System.Drawing;
using SAC.Helper;
using System.Text.RegularExpressions;
using System.Text;

public partial class bgData_DLManage : System.Web.UI.Page
{
    DataTable dt = new DataTable();

    BLL.BLLData bd = new BLLData();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.Bind();
        }
    }

    protected void GridView2_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView2.EditIndex = -1;
        this.GridView2.DataBind();
    }

    protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string mon = ((TextBox)this.GridView2.Rows[0].FindControl("txtMonth")).Text;

        if (mon.Contains("'"))
            mon = mon.Replace("'", "''");

        string sql = "";
        sql = @"insert into 实时计算测点表 (测点ID,测点描述,计算公式,公式参数,参数下限,参数上限,计算类型,优先级) values('"
            + ((TextBox)this.GridView2.Rows[0].FindControl("txtPointID")).Text + "','"
             + ((TextBox)this.GridView2.Rows[0].FindControl("txtDesc")).Text + "','"
              + mon + "','"
               + ((TextBox)this.GridView2.Rows[0].FindControl("txtPara")).Text + "',"
               + ((TextBox)this.GridView2.Rows[0].FindControl("txtLow")).Text + ","
               + ((TextBox)this.GridView2.Rows[0].FindControl("txtUp")).Text + ",'"
               + ((TextBox)this.GridView2.Rows[0].FindControl("txtType")).Text + "',"
            + ((TextBox)this.GridView2.Rows[0].FindControl("txtLevel")).Text
            + ");";

        if (bd.RunNonQuery(sql) == true)
        {
            GridView2.EditIndex = -1;
            //num = GridView1.PageSize * GridView1.PageIndex;
            this.Bind();
            this.GridView2.DataBind();
        }
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        dt = bd.RetRTPointTable();

        GridView1.PageIndex = e.NewPageIndex;

        this.GridView1.DataSource = dt;
        this.GridView1.DataBind();

        //设置格式
        GridView1.Attributes.Add("BorderColor", "lightBlue");
    }

    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        //num = GridView1.PageSize * GridView1.PageIndex;
        this.Bind();
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        if (GridView2.Rows.Count > 0)
        {
            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "onekey", "alert('正在进行添加，禁止删除！')", true);
        }
        else if (GridView1.EditIndex != -1)
        {
            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "onekey", "alert('正在进行编辑，禁止删除！')", true);
        }
        else
        {
            string sql = "";
            sql = @"delete from 实时计算测点表 where ID_KEY = " + GridView1.DataKeys[e.RowIndex].Values[0];

            if (bd.RunNonQuery(sql) == true)
                this.Bind();
        }
    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        if (GridView2.Rows.Count > 0)
            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "onekey", "alert('正在进行添加，禁止编辑！')", true);
        else
        {
            GridView1.EditIndex = e.NewEditIndex;

            //当前编辑行背景色高亮
            this.GridView1.EditRowStyle.BackColor = Color.FromName("#F7CE90");

            this.Bind();
        }
    }

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string mon = ((TextBox)this.GridView1.Rows[e.RowIndex].FindControl("month")).Text;

        if (mon.Contains("'"))
            mon = mon.Replace("'", "''");

        string sql = "";
        sql = @"update  实时计算测点表 set 测点ID = '" + ((TextBox)this.GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text
            + "', 测点描述 = '" + ((TextBox)this.GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text
            + "', 计算公式 = '" + mon
            + "', 公式参数 = '" + ((TextBox)this.GridView1.Rows[e.RowIndex].FindControl("para")).Text
        + "', 参数下限 = " + ((TextBox)this.GridView1.Rows[e.RowIndex].Cells[6].Controls[0]).Text
        + ", 参数上限 = " + ((TextBox)this.GridView1.Rows[e.RowIndex].Cells[5].Controls[0]).Text
        + ", 计算类型 = '" + ((TextBox)this.GridView1.Rows[e.RowIndex].Cells[7].Controls[0]).Text
        + "', 优先级 = '" + ((TextBox)this.GridView1.Rows[e.RowIndex].Cells[8].Controls[0]).Text
        + "' where ID_KEY =  " + GridView1.DataKeys[e.RowIndex].Values[0];

        if (bd.RunNonQuery(sql) == true)
        {
            GridView1.EditIndex = -1;

            this.Bind();
        }
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //加入鼠标滑过的高亮效果
        if (e.Row.RowType == DataControlRowType.DataRow)//判定当前的行是否属于datarow类型的行
        {
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#E4F6FF',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
        }
    }

    protected void LBtnAdd_Click(object sender, EventArgs e)
    {
        if (GridView2.Rows.Count > 0)
        {
        }
        else if (GridView1.EditIndex != -1)
        {
            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "onekey", "alert('正在进行编辑，禁止添加！')", true);
        }
        else
        {
            if (this.GridView1.Rows.Count < 1)
                this.GridView2.ShowHeader = true;
            else
                this.GridView2.ShowHeader = false;

            DataTable dt2 = this.GetDataFromGrid();
            DataRow newRow = dt2.NewRow();

            dt2.Rows.Add(newRow);
            this.GridView2.DataSource = dt2;
            this.GridView2.DataBind();
        }
    }

    protected DataTable GetDataFromGrid()
    {
        DataTable dt1 = new DataTable("Table1");
        dt1.Columns.Add("ID_KEY");
        dt1.Columns.Add("CORPID");
        dt1.Columns.Add("CORPNAME");
        for (int i = 0; i < GridView2.Rows.Count; i++)
        {
            GridViewRow gRow = GridView2.Rows[i];
            DataRow newRow = dt1.NewRow();
            newRow[0] = GridView2.DataKeys[i].Values[0];
            newRow[1] = GridView2.DataKeys[i].Values[1];
            newRow[2] = GridView2.DataKeys[i].Values[2];
            //newRow[3] = GridView2.DataKeys[i].Values[3];
            //newRow[4] = GridView2.DataKeys[i].Values[4];
            //newRow[5] = GridView2.DataKeys[i].Values[5];
            //newRow[6] = GridView2.DataKeys[i].Values[6];
            //newRow[7] = GridView2.DataKeys[i].Values[7];
            //newRow[8] = GridView2.DataKeys[i].Values[8];

            //(gRow.Cells[0].Controls[0] as TextBox).ReadOnly = true; 
            //newRow[1] = (gRow.Cells[1].Controls[0] as TextBox).Text;
            //newRow[2] = (gRow.Cells[2].Controls[0] as TextBox).Text;
            dt1.Rows.Add(newRow);
        }
        dt1.AcceptChanges();
        return dt1;
    }

    private void Bind()
    {
        dt = bd.RetRTPointTable();

        if (dt.Rows.Count > 0)
        {
            this.GridView1.DataSource = dt;
            this.GridView1.DataBind();

            //设置格式
            GridView1.Attributes.Add("BorderColor", "lightBlue");
        }
        else
        {
            this.GridView1.DataSource = dt;
            this.GridView1.DataBind();
        }

        GridView1.DataKeyNames = new string[] { "ID_KEY" };
    }

    public string GetFirstString(string stringToSub, int length)
    {
        Regex regex = new Regex("[\u4e00-\u9fa5]+", RegexOptions.Compiled);
        char[] stringChar = stringToSub.ToCharArray();
        StringBuilder sb = new StringBuilder();
        int nLength = 0;
        bool isCut = false;
        for (int i = 0; i < stringChar.Length; i++)
        {
            if (regex.IsMatch((stringChar[i]).ToString()))
            {
                sb.Append(stringChar[i]);
                nLength += 2;
            }
            else
            {
                sb.Append(stringChar[i]);
                nLength = nLength + 1;
            }

            if (nLength > length)
            {
                isCut = true;
                break;
            }
        }
        if (isCut)
            return sb.ToString() + ". . .";
        else
            return sb.ToString();

    }
}
